package com.example.photo_sharing.tool.Impl;

import android.os.NetworkOnMainThreadException;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.photo_sharing.R;
import com.example.photo_sharing.global.App;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class FocusLikeCollectImpl implements com.example.photo_sharing.tool.FocusLikeCollectTool {

    private App app = new App();

    @Override
    public boolean setLike(String shareId, String userId) {
        new Thread(() -> {

            // url路径
            String url = "http://47.107.52.7:88/member/photo/like?"
                    +"shareId="+shareId
                    +"&userId="+userId;

            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", app.getAppId())
                    .add("appSecret", app.getAppSecret())
                    .add("Content-Type", "application/json")
                    .build();


            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, ""))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
        return true;
    }

    @Override
    public boolean setCollect(String shareId, String userId) {
        new Thread(() -> {

            // url路径
            String url = "http://47.107.52.7:88/member/photo/collect?"
                    +"shareId="+shareId
                    +"&userId="+userId;

            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", app.getAppId())
                    .add("appSecret", app.getAppSecret())
                    .add("Content-Type", "application/json")
                    .build();


            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, ""))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
        return true;
    }

    @Override
    public boolean setFocus(String pId, String userId) {
        new Thread(() -> {

            // url路径
            String url = "http://47.107.52.7:88/member/photo/focus?"
                    +"focusUserId="+pId
                    +"&userId="+userId;
            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", app.getAppId())
                    .add("appSecret", app.getAppSecret())
                    .add("Content-Type", "application/json")
                    .build();


            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, ""))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
        return true;
    }

    @Override
    public boolean deLike(String likeId) {
        new Thread(() -> {

            // url路径
            String url = "http://47.107.52.7:88/member/photo/like/cancel?"+
                    "likeId="+likeId;

            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", app.getAppId())
                    .add("appSecret", app.getAppSecret())
                    .add("Content-Type", "application/json")
                    .build();


            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, ""))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
        return true;
    }

    @Override
    public boolean deCollect(String collectId) {
        new Thread(() -> {

            // url路径
            String url = "http://47.107.52.7:88/member/photo/collect/cancel?"
                    +"collectId="+collectId;

            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", app.getAppId())
                    .add("appSecret", app.getAppSecret())
                    .add("Content-Type", "application/json")
                    .build();


            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, ""))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
        return true;
    }

    @Override
    public boolean deFocus(String pId,String UserId) {
        new Thread(() -> {

            // url路径
            String url = "http://47.107.52.7:88/member/photo/focus/cancel?"
                    +"focusUserId="+pId
                    +"&userId="+UserId;

            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", app.getAppId())
                    .add("appSecret", app.getAppSecret())
                    .add("Content-Type", "application/json")
                    .build();


            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, ""))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
        return true;
    }

    @Override
    public boolean setImgLike(ImageButton bt, TextView tv) {
        Picasso.get().load(app.getImg_like_true()).into(bt);
        int num = Integer.parseInt(tv.getText().toString());
        num ++;
        tv.setText(num+"");
        return true;
    }

    @Override
    public boolean setImgDelike(ImageButton bt, TextView tv) {
        Picasso.get().load(app.getImg_like_false()).into(bt);
        int num = Integer.parseInt(tv.getText().toString());
        num --;
        tv.setText(num+"");
        return false;
    }

    @Override
    public boolean setImgCollect(ImageButton bt, TextView tv) {
        Picasso.get().load(R.mipmap.collect).into(bt);
        int num = Integer.parseInt(tv.getText().toString());
        num ++;
        tv.setText(num+"");
        return true;
    }

    @Override
    public boolean setImgDecollect(ImageButton bt, TextView tv) {
        Picasso.get().load(R.mipmap.uncollect).into(bt);
        int num = Integer.parseInt(tv.getText().toString());
        num --;
        tv.setText(num+"");
        return false;
    }

    @Override
    public boolean setImgFocus(Button bt) {
        bt.setText("已关注");
        return true;
    }

    @Override
    public boolean setImgDefocus(Button bt) {
        bt.setText("关注+");
        return false;
    }

    private final Callback callback = new Callback() {
        @Override
        public void onFailure(@NonNull Call call, IOException e) {
            e.printStackTrace();
            System.out.println("失败");
        }

        @Override
        public void onResponse(@NonNull Call call, Response response) throws IOException {
            String responseData = response.body().string();
            System.out.println(responseData);
            System.out.println("成功");
        }
    };
}
